package base
{
	import com.dusk.util.ArrayUtil;
	import com.dusk.util.ClassUtil;
	import com.game.engine.nape.BodyTypeConst;
	import com.game.engine.nape.utils.CBTypes;
	import com.game.engine.nape.utils.CollisionGroup;
	import com.game.engine.phys.Collision2D;
	import com.game.engine.phys.RigidBody;
	import com.game.engine.phys.Vector2D;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import nape.phys.Material;
	
	public class BaseTerrain extends BaseObject
	{
		
		public function BaseTerrain()
		{
			super();
			setStatic();
			gravity = 0;
//			rotation = Math.round(rotation);
//			var _loc1_:Number = height;
//			var _loc2_:Number = width;
//			if (rotation != 90)
//			{
//				equationX = Math.tan(rotation * Math.PI / 180);
//			}
//			else
//			{
//				equationX = 1;
//			}
//			equationX2 = -1 / Number(equationX);
//			var _loc3_:Number = _loc1_ / 2 / Math.cos(Math.abs(rotation * Math.PI / 180));
//			constX = -Number(equationX) * x + y - _loc3_;
//			if (rotation < 0)
//			{
//				maxX = (y - height / 2 - Number(constX)) / Number(equationX);
//				minX = x - width / 2;
//			}
//			else
//			{
//				maxX = x + width / 2;
//				minX = (y - height / 2 - Number(constX)) / Number(equationX);
//			}
		}
		
		protected var equationX:Number;
		protected var constX:Number;
		protected var equationX2:Number;
		protected var sourceRotation:int;
		protected var minX:Number;
		protected var maxX:Number;
		protected var texture:BaseAnimation;
		
		public var wallType:int;
		
		
		public function buildFromSprite(sp:Sprite):void
		{
			if(!sp.parent) return;
			var box:Rectangle = sp.getBounds(sp.parent);
			collideBox = new Rectangle(0, 0, box.width, box.height);
			x = box.x;
			y = box.y;
			sp.parent.addChildAt(this, sp.parent.getChildIndex(sp));
			sp.parent.removeChild(sp);
			initCollideArea();
			CONFIG::DEBUG{
				graphics.beginFill(0,1);
				graphics.drawRect(0,0,sp.width,sp.height);
				graphics.endFill();
			}
		}
		
		public function setTexture(tex:BaseAnimation):void
		{
			texture = tex;
			addChild(tex);
		}
		
		override public function setStatic():void
		{
			velocity = Vector2D.zero;
		}
		
		public function setYByRole(param1:*):Point
		{
			var _loc4_:Point = new Point();
			var _loc5_:Number;
			if ((_loc5_ = Number(equationX) * Number(param1.x) + constX) > param1.y)
			{
				param1.standInObj = this;
			}
			if (!param1.isWalkOrRun() && !param1.isBeAttacking() && !param1.isCanMoveWhenAttack())
			{
				return _loc4_;
			}
			var _loc6_:Rectangle = param1.getNextFrameXBounds();
			var _loc7_:Number = Math.abs(Math.cos(rotation * Math.PI / 180) * Number(param1.speed.x));
			var _loc8_:Number = Math.tan(rotation * Math.PI / 180) * _loc7_;
			if (!isOutOfThisLine(param1) || isOutOfThisLine(param1) && getBounds(gc.gameScene).intersects(param1.getNextFrameXBounds()))
			{
				if (param1.speed.x >= 0)
				{
					if (rotation < 0)
					{
						_loc8_ = -Math.abs(_loc8_);
					}
					else
					{
						_loc8_ = Math.abs(_loc8_);
					}
				}
				else
				{
					_loc7_ = -_loc7_;
					if (rotation < 0)
					{
						_loc8_ = Math.abs(_loc8_);
					}
					else
					{
						_loc8_ = -Math.abs(_loc8_);
					}
				}
				if (param1 is BaseHero)
				{
					if (param1.isCanMoveByStage())
					{
						_loc4_.x = _loc7_;
						_loc4_.y = _loc8_;
					}
				}
				else
				{
					_loc4_.x = _loc7_;
					_loc4_.y = _loc8_;
				}
			}
			return _loc4_;
		}
		
		public function isOutOfThisLine(param1:BaseObject):Boolean
		{
			var rect:Rectangle = param1.getNextFrameCollideArea();
			if (rotation < 0)
			{
				return !(rect.x + rect.width >= minX && rect.x + rect.width <= maxX);
			}
			return !(rect.x >= minX && rect.x + rect.width <= maxX);
		}
		
		override public function destroy():void
		{
			removeFromParent();
			isReadyToDestroy = true;
		}
		
		public function getMirrorVector():Point
		{
			if (Math.round(rotation) != 90)
			{
				return new Point(1, equationX);
			}
			return new Point(0, equationX);
		}
		
		override public function isStatic():Boolean
		{
			return velocity.x == 0 && velocity.y == 0;
		}
	}
}
